/*
 * @Author: hcluo
 * @Date: 2020-08-07 15:31:48
 * @LastEditors: hcluo
 * @LastEditTime: 2020-08-07 16:23:17
 * @Description: 政府项目，页面配置处理程序，上传页面配置致主站和开发站。！！慎用。使用前请检查
 * src/features/pages/redux/pageConfig/index.js文件，保证只有需要更新的文件被引入，其他文件均被注释
 */
'use strict';

import * as pageConfigs from '../src/features/pages/redux/pageConfig/index.js';
import random from 'string-random';
import fetch from 'node-fetch';
import async from 'async';
import _ from 'lodash';

const createCode = () => random(8) + new Date().getTime();
const createPageCode = () => random(8, { numbers: false, letters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' });

const hostMain = 'https://180.96.8.44'; //"http://10.100.7.61:12202"
const hostDev = 'http://10.102.18.199:12204';

const request2 = ({ url, method = 'POST', params, body }, host) => {
	let opt = {
		method,
		headers: {
			'Content-Type': 'application/json',
		},
	};

	url += '?wind.sessionid=' + '7488041e55854df9991613615f0cc5b4';

	if (body) {
		opt = {
			method,
			headers: {
				'Content-Type': 'application/json',
			},
			body: encodeURI(JSON.stringify(body)),
		};
	}

	if (params) {
		for (const key in params) {
			url += `&${key}=${JSON.stringify(params[key])}`;
		}
	}

	return fetch(host + url, opt)
		.then(response => {
			if (!response.ok) {
				return Promise.reject(response.status);
			}
			return response.json();
		})
		.then(res => {
			if (res.resultCode === '200') return res.resultData;
			else {
				console.error('服务端错误：', host + url, res);
				return Promise.reject(res.resultMessage);
			}
		});
};

Object.keys(pageConfigs).map((fileName, index) => {
	if (fileName === 'collections') return;

	let page = pageConfigs[fileName];
	// page.pageName = fileName.split('_')[1]
	let newFileName = createPageCode();
	let readme = ``;
	let table = '|模块名称| 前端 | 后端 |\n| ------ | -- | -- |';
	// page.code = newFileName

	// json处理
	page.outConfigs.map(outConfig => {
		// outConfig.code = createCode()

		// if (outConfig.hasOwnProperty('style')) {
		//     outConfig.style = Object.assign(outConfig.style, { padding: '6px' })
		// } else {
		//     outConfig.style = { padding: '6px' }
		// }

		outConfig.middleConfigs.map(middleConfig => {
			// middleConfig.code = createCode()
			// if (middleConfig.hasOwnProperty('requestConf')) {
			//     let req = handleRequestParams(middleConfig.requestConfig, null, {
			//         pageData: { indicator: page.data.indicator },
			//         locationV2: '0301010000'
			//     })
			//     requestConfigs.push({
			//         req,
			//         title: `【${page.pageName}】${outConfig.title}`
			//     })
			// }

			middleConfig.basicConfigs.map(basicConfig => {
				// if (basicConfig.type === "Table") {
				//     if (basicConfig.dataHandleConf && basicConfig.dataHandleConf[0].rule) {
				//         let newRule = basicConfig.dataHandleConf[0].rule.filter(item => {
				//             if (item.hasOwnProperty('divide') || item.hasOwnProperty('toFixed')) {
				//                 basicConfig.displayConf.showColumns.filter(column => {
				//                     if (column.dataIndex === item.key) {
				//                         let rule = {}
				//                         if (item.hasOwnProperty('divide')) {
				//                             rule.divide = item.divide
				//                         }
				//                         if (item.hasOwnProperty('toFixed')) {
				//                             rule.toFixed = 2
				//                         }
				//                         column.rule = rule
				//                     }
				//                 })
				//             } else {
				//                 return item
				//             }
				//         })
				//         basicConfig.dataHandleConf[0].rule = newRule
				//     }
				// }
				// if (basicConfig.hasOwnProperty('requestConf')) {
				//     let req = handleRequestParams(basicConfig.requestConfig, null, {
				//         pageData: { indicator: page.data.indicator },
				//         locationV2: '0301010000'
				//     })
				//     requestConfigs.push({
				//         req,
				//         title: `【${page.pageName}】${outConfig.title}`
				//     })
				// }
			});
		});
	});

	let fileString = `const initialState = ${JSON.stringify(
		page,
		null,
		4,
	)}\n\nexport default initialState;`;

	// fs.writeFileSync(`${dir}/${page.pageName}.js`, fileString)

	// 发送到服务端

	let requestBody = {
		business: page,
	};

	false &&
		request2({ url: '/govwebapi/gov/web/api/stdpage/config/add', body: requestBody }, hostMain)
			.then(res => {
				console.log('【主  站】：', page.pageName, '更新完成', res.code);
			})
			.catch(err => {
				console.log(`\n\n\n【主  站】：${page.pageName} err`, err, '\n\n\n');
			});

	request2({ url: '/govwebapi/gov/web/api/stdpage/config/add', body: requestBody }, hostDev)
		.then(res => {
			console.log('【开发站】：', page.pageName, '更新完成', res.code);
		})
		.catch(err => {
			console.log(`\n\n\n【开发站】：${page.pageName} err`, err, '\n\n\n');
		});
});
